package com.briup.jdk8.day01;

import com.briup.corejava.day10.pojo.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * 常用的函数式接口
 * Predicate
 * 要求，传入一个任意类型，返回boolean
 */
public class Test3 {
    public static void main(String[] args) {
        Predicate<Integer> p = s->s>50;
        Predicate<Integer> p1 = s->s<20;
        Integer[] arr = new Integer[]{2,52,6,3,6,2};
        System.out.println(arrayFilter(arr, p.or(p1).and(p).negate()));

        String[] strings = new String[]{"C","C++","C#","Java","JS"};
        List<String> list = arrayFilter(strings, s -> s.startsWith("C"));
        list.forEach((s)->{
            System.out.println(s);
        });

        Student[] students = new Student[]{new Student("张三",20),new Student("李四",21)};
        System.out.println(arrayFilter(students, s -> s.getAge() == 20));



    }
    public static <T> List<T> arrayFilter(T[] arr, Predicate<T> p){
        List<T> list = new ArrayList<>();
        for(T i : arr){
            if(p.test(i)){
                list.add(i);
            }
        }
        return list;
    }
}
